package ym.controller.back.category;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.CategoryType;
import ym.service.CategoryTypeService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台广告类型
 */
@RestController
@CrossOrigin
@RequestMapping("/back/categoryType")
public class CategoryTypeCon {
    @Autowired
    private CategoryTypeService categoryTypeService;

    /**
     * 新增
     *
     * @param categoryType
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/category')")
    public BaseResult add(CategoryType categoryType) {
        categoryTypeService.add(categoryType);
        return BaseResult.ok();
    }

    /**
     * 修改
     *
     * @param categoryType
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(CategoryType categoryType) {
        categoryTypeService.update(categoryType);
        return BaseResult.ok();
    }

    /**
     * 删除
     *
     * @param categoryTypeId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer categoryTypeId) {
        categoryTypeService.delete(categoryTypeId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询
     *
     * @param categoryTypeId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer categoryTypeId) {
        CategoryType categoryType = categoryTypeService.findById(categoryTypeId);
        return BaseResult.ok(categoryType);
    }
    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<CategoryType> categoryTypes = categoryTypeService.findAll();
        return BaseResult.ok(categoryTypes);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @PreAuthorize("hasAnyAuthority('/category')")
    @GetMapping("/search")
    public BaseResult search(int page, int size) {
        Page<CategoryType> brandPage = categoryTypeService.search(page, size);
        return BaseResult.ok(brandPage);
    }
}
